﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;

namespace TaxiDispatchClient
{
    public partial class TaxiOnlineTimeForm : DevExpress.XtraEditors.XtraForm
    {
        public TaxiOnlineTimeForm()
        {
            InitializeComponent();

            if (MainForm.MyUserInfo.CompanyName != Service.PowerConfig.MainCompanyName)
            {
                int count = imageComboBoxEdit1.Properties.Items.Count;
                imageComboBoxEdit1.Properties.Items.RemoveAt(3);
                imageComboBoxEdit1.Properties.Items.RemoveAt(2);
            }

            dateEdit1.DateTime = DateTime.Today.AddDays(-1);
            dateEdit2.DateTime = DateTime.Today.AddDays(-1);
            
            Thread init = new Thread(InitControl);
            init.IsBackground = true;
            init.Start();

            Thread tbind = new Thread(BindData);
            tbind.IsBackground = true;
            tbind.Start();
        }

        private DataTable _dtGroup;
        private List<string> _companylist;

        private void InitControl()
        {           
            DataSet ds = null;
            do
            {
                ds = MainForm.PositionService.TaxiGroupGetList("");
                Thread.Sleep(500); 
            } while (ds==null || ds.Tables.Count==0);
            _dtGroup = ds.Tables[0];

            List<string> company;
            do
            {
                company = MainForm.PositionService.GetCompanyList();
            } while (company==null);
            _companylist = company;

        }

        private void BindData()
        {
          
            string date1 = dateEdit1.DateTime.ToString("yyyy-MM-dd");
            string date2 = dateEdit2.DateTime.ToString("yyyy-MM-dd");
            string para = "";
            string paraValue = "";             
            switch (imageComboBoxEdit1.SelectedIndex)
            {
                case 0:
                    break;
                case 1:
                    para = imageComboBoxEdit1.Properties.Items[
                    imageComboBoxEdit1.SelectedIndex].Value.ToString();
                    paraValue = btnText_phoneNumber.Text;
                    break;
                default :
                    para = imageComboBoxEdit1.Properties.Items[
                    imageComboBoxEdit1.SelectedIndex].Value.ToString();    
                    paraValue = imageComboBox_list.Properties.Items[
                    imageComboBox_list.SelectedIndex].Value.ToString();
                    break;
            }

            if (MainForm.MyUserInfo.CompanyName!=Service.PowerConfig.MainCompanyName)
            {
                if (para!="")
                {
                    paraValue += "' ";
                }
                paraValue += " and  CompanyName like '" +
                        MainForm.MyUserInfo.CompanyName + "%'";

               
            }

            try
            {
                DataSet ds = MainForm.PositionService.TaxiOnlineTimeGetListWhere(
                    date1, date2, paraValue);
                if (ds != null && ds.Tables.Count > 0)
                {
                //    switch (imageComboBoxEdit1.SelectedIndex)
                //    {
                //        //case 0:
                //        //    gridView1.Columns["Date"].Visible = false;
                //        //    break;
                //        //case 1:                          
                //        //    break;
                //        //default:
                //        //    gridView1.Columns["Date"].Visible = false;
                //        //    break;

                //        case 1://单辆车
                //            break;
                //        default:
                //            string strDate1 = dateEdit1.DateTime.ToString("yyyy-MM-dd");
                //            string strDate2 = dateEdit2.DateTime.ToString("yyyy-MM-dd");
                //            TimeSpan span = dateEdit2.DateTime.Date - dateEdit1.DateTime.Date;
                //            if (strDate1 != date2)
                //            {
                //                foreach (DataRow row in ds.Tables[0].Rows)
                //                {
                //                    row["Date"] = strDate1 + "至" + strDate2
                //                        +" (共"+(span.TotalDays+1)+"天)";
                //                }
                //            }
                //            break;
                //    }       
                     
                    BeginInvoke(new MethodInvoker(() =>
                        {
                            gridControl1.DataSource = ds.Tables[0];
                            gridView1.GroupPanelText = "统计结果";
                            btn_select.Enabled = true;
                        }));

                    
                }                
            }
            catch (Exception ex)
            {
                BeginInvoke(new MethodInvoker(() =>
                {                    
                    btn_select.Enabled = true;
                }));
                Common.CommonRoutine.HandlingExceptions(ex,true);
            }
        }



        private void imageComboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (imageComboBoxEdit1.SelectedIndex)
            {
                case 0:
                    imageComboBox_list.Visible = false;
                    btnText_phoneNumber.Visible = false;
                    break;
                case 1:
                    imageComboBox_list.Visible = false;
                    btnText_phoneNumber.Visible = true;
                    btnText_phoneNumber.Text = "";
                    btnText_phoneNumber.Location = imageComboBox_list.Location;
                    break;
                case 2:
                    imageComboBox_list.Visible = true;
                    btnText_phoneNumber.Visible = false;
                    imageComboBox_list.Properties.Items.Clear();
                    if (_dtGroup!=null)
                    {
                        foreach (DataRow row in _dtGroup.Rows)
                        {
                            ImageComboBoxItem item = new ImageComboBoxItem();
                            item.Description = row["TaxiGroup"].ToString();
                            item.Value = row["Id"].ToString();
                            imageComboBox_list.Properties.Items.Add(item);
                        }
                    }
                    else
                    {
                        XtraMessageBox.Show("未能成功获取到出租车分组列表！");
                    }
                    break;
                case 3:
                    imageComboBox_list.Visible = true;
                    btnText_phoneNumber.Visible = false;
                    imageComboBox_list.Properties.Items.Clear();
                    if (_companylist!=null)
                    {
                        foreach (string company in _companylist)
                        {
                            ImageComboBoxItem item = new ImageComboBoxItem();
                            item.Description = company;
                            item.Value = company;
                            imageComboBox_list.Properties.Items.Add(item);
                        }
                    }
                    else
                    {
                        XtraMessageBox.Show("未能成功获取到出租车公司列表！");
                    }
                    break;
            }
        }

        private void btn_select_Click(object sender, EventArgs e)
        {
            btn_select.Enabled = false;
            if (!CheckPass())
            {                 
                btn_select.Enabled = true;                 
                return;
            }
            gridView1.GroupPanelText = "数据查询中...";
            Thread t = new Thread(BindData);
            t.IsBackground = true;
            t.Start();
        }


        private void btn_exportToExcel_MouseClick(object sender, MouseEventArgs e)
        {

            saveFileDialog1.FileName = "车辆程序在线时间统计";
            saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                gridView1.ExportToXls(saveFileDialog1.FileName);
                XtraMessageBox.Show("Excel导出完成！");
            }
        }

        private bool CheckPass()
        {
            switch (imageComboBoxEdit1.SelectedIndex)
            {
                case 1:
                    if (btnText_phoneNumber.Text.Trim() == "")
                    {
                        XtraMessageBox.Show("请输入要查询的车辆的手机号码！");
                        btnText_phoneNumber.Focus();
                        return false;
                    }
                    break;
                case 2:
                    if (imageComboBox_list.SelectedIndex==-1)
                    {
                        XtraMessageBox.Show("请选择要查询的分组！");
                        imageComboBox_list.Focus();
                        return false;
                    }
                    break;
                case 3:
                    if (imageComboBox_list.SelectedIndex==-1)
                    {
                        XtraMessageBox.Show("请选择要查询的公司！");
                        imageComboBox_list.Focus();
                        return false;
                    }
                    break;
            }
            return true;
        }


        #region 窗体控件效果
        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }

        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;

            Cursor = Cursors.Arrow;

        }
        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void pictureEdit_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void PhoneUserForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Close();
            }
        }
        #endregion

        private void btnText_phoneNumber_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            DriverInfoForm form = new DriverInfoForm();
            form.ShowDialog();
            DataRow row = form.gridView1.GetDataRow(form.gridView1.FocusedRowHandle);
            if (row != null)
            {
                btnText_phoneNumber.Text = row["PhoneNumber"].ToString();                
            }
        }

    }
}